package com.hibase.core.interfacetask.entity.vo;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hibase.core.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * 接口类型缓存
 *
 * @author zhou_jw
 * @create 2020-08-25 20:09
 */
@Repository
@Slf4j
public class InterfaceTypeCache {

    @Autowired
    private RedisUtil redisUtil;

    // base key
    public final static String BASE_KEY = "cap";

    // 模块
    public final static String INTERFACE_TYPE = "interface_type";

    public final static String SPLIT = ":";


    public final static int EXPIRE_TIME = 60 * 60 * 24 * 3;


    /**
     * 保存interface_type信息
     */
    public void saveInterfaceType(InterfaceTypeCacheVO entity) {
        if (StringUtils.isEmpty(entity)) {
            return;
        }
        String key = getKey();
        redisUtil.hPut(key,entity.getBusinessTypeCode(),entity);
        redisUtil.expire(key, EXPIRE_TIME, TimeUnit.SECONDS);
    }

    /**
     * 保存interface_type信息
     */
    public void saveInterfaceType(InterfaceTypeCacheVO entity, int expireTime, TimeUnit unit) {
        if (StringUtils.isEmpty(entity)) {
            return;
        }
        String key = getKey();
        redisUtil.hPut(key,entity.getBusinessTypeCode(), entity);
        redisUtil.expire(key, EXPIRE_TIME, TimeUnit.SECONDS);
    }

    /**
     * 获取interface_type信息
     *
     * @return
     */
    public InterfaceTypeCacheVO getInterfaceType(String bussinessTypeCode) {
        String key = getKey();
        Object result = redisUtil.hGet(key, bussinessTypeCode);
        if (ObjectUtil.isNotEmpty(result)  && result instanceof InterfaceTypeCacheVO) {
            return (InterfaceTypeCacheVO) result;
        } else {
            return new InterfaceTypeCacheVO();
        }
    }

    /**
     * 删除interface_type信息
     *
     * @return
     */
    public void delInterfaceType(String bussinessTypeCode) {
        if (StrUtil.isNotBlank(bussinessTypeCode)) {
            String key = getKey();
            redisUtil.hDelete(key,bussinessTypeCode);
        }
    }


//    /**
//     * 缓存用户修改密码信息
//     */
//    public void addChangePwdToken(String token, String loginName) {
//
//        ValueOperations<String, String> value = redisTemplate.opsForValue();
//
//        String changePwdKey = getChangePwdKey(token);
//
//        value.set(changePwdKey, loginName);
//
//        redisTemplate.expire(changePwdKey, CHANGE_EXPIRE_TIME, TimeUnit.SECONDS);
//    }

    private String getKey() {

        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append(BASE_KEY);
        stringBuilder.append(SPLIT);
        stringBuilder.append(INTERFACE_TYPE);

        return stringBuilder.toString();
    }
}
